package top.goldenyear.porpoise.common.core.util.date;

import cn.hutool.v7.core.date.TimeUtil;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 时间工具类，用于 {@link LocalDateTime}
 *
 * @author Airhead
 */
public class TimeUtils extends TimeUtil {

  /**
   * 空的 LocalDateTime 对象，主要用于 DB 唯一索引的默认值
   */
  public static LocalDateTime EMPTY = buildTime(1970, 1, 1);

  public static LocalDateTime addTime(Duration duration) {
    return LocalDateTime.now().plus(duration);
  }

  public static boolean beforeNow(LocalDateTime date) {
    return date.isBefore(LocalDateTime.now());
  }

  public static boolean afterNow(LocalDateTime date) {
    return date.isAfter(LocalDateTime.now());
  }

  /**
   * 创建指定时间
   *
   * @param year  年
   * @param mouth 月
   * @param day   日
   * @return 指定时间
   */
  public static LocalDateTime buildTime(int year, int mouth, int day) {
    return LocalDateTime.of(year, mouth, day, 0, 0, 0);
  }

  public static LocalDateTime[] buildBetweenTime(
      int year1, int mouth1, int day1, int year2, int mouth2, int day2) {
    return new LocalDateTime[]{buildTime(year1, mouth1, day1), buildTime(year2, mouth2, day2)};
  }

  /**
   * 判断当前时间是否在该时间范围内
   *
   * @param startTime 开始时间
   * @param endTime   结束时间
   * @return 是否
   */
  public static boolean isBetween(LocalDateTime startTime, LocalDateTime endTime) {
    if (startTime == null || endTime == null) {
      return false;
    }
    return TimeUtil.isIn(LocalDateTime.now(), startTime, endTime);
  }
}
